package ro.gateway.aida.fnd;

import java.sql.SQLException;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;

import ro.gateway.aida.admin.PropertyListDB;
import ro.gateway.aida.admin.PropertyListItem;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.obj.Activity;
import ro.gateway.aida.obj.Currency;
import ro.gateway.aida.obj.Person;
import ro.gateway.aida.obj.db.PersonDB;
import ro.gateway.aida.utils.HttpUtils;
import ro.xblue.translator.TranslatorUtils;


/**
 * <p>Title: Romanian AIDA</p> <p>Description: </p> <p>Copyright: Copyright (comparator) 2003</p> <p>Company:
 * ro-gateway</p>
 *
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: FundingForm.java,v 1.1 2004/10/24 23:37:14 mihaipostelnicu Exp $
 */
public class FundingForm extends ActionForm {
	FundingItem item;
	String key;
	int item_idx = -1;
	String event;
	String action;
	long[] personIds;
	ArrayList typesofassistance;
	ArrayList termsofassistance;
	ArrayList statuses;
	Currency[] currencies;

	public Currency[] getCurrencies() {
		return currencies;
	}

	public void setCurrencies(Currency[] currencies) {
		this.currencies = currencies;
	}

	public ArrayList getStatuses() {
		return statuses;
	}

	public void setStatuses(ArrayList statuses) {
		this.statuses = statuses;
	}

	public void setTypeofassistanceid(int id) {
		item.setType_of_assistance(null);
		for (int i = 0; i < typesofassistance.size(); i++) {
			PropertyListItem type = (PropertyListItem) typesofassistance.get(i);
			if (type.getId() == id) {
				item.setType_of_assistance(type);
			}
		}
	}

	public void setTermofassistanceid(int id) {
		item.setTerms_of_assistance(null);
		for (int i = 0; i < termsofassistance.size(); i++) {
			PropertyListItem term = (PropertyListItem) termsofassistance.get(i);
			if (term.getId() == id) {
				item.setTerms_of_assistance(term);
			}
		}
	}

	public long getTypeofassistanceid() {
		if (item.getType_of_assistance() == null) return -1;
		return item.getType_of_assistance().getId();
	}

	public long getTermofassistanceid() {
		if (item.getTerms_of_assistance() == null) return -1;
		return item.getTerms_of_assistance().getId();
	}

	public ArrayList getTypesofassistance() {
		return typesofassistance;
	}

	public void setTypesofassistance(ArrayList typesofassistance) {
		this.typesofassistance = typesofassistance;
	}

	public ArrayList getTermsofassistance() {
		return termsofassistance;
	}

	public void setTermsofassistance(ArrayList termsofassistance) {
		this.termsofassistance = termsofassistance;
	}

	public FundingForm() {
		item = new FundingItem();
	}

	public String getComment() {
		return item.getComments();
	}

	public void setComment() {
	};

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public String getEvent() {
		return event;
	}

	public void setEvent(String event) {
		this.event = event;
	}

	public FundingItem getItem() {
		return item;
	}

	public void setItem(FundingItem item) {
		this.item = item;
	}

	public int getItem_idx() {
		return item_idx;
	}

	public void setItem_idx(int item_idx) {
		this.item_idx = item_idx;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public void setOrgId(long org_id) {
		item.setOrg_id(org_id);
	}

	public long getOrgId() {
		return item.getOrg_id();
	}

//     public void setFundingType( long type_id ) {
//          item.setType_id( type_id );
//     }
//
//     public long getFundingType() {
//          return item.getType_id();
//     }

	public void setStatus(long status) {
		item.setStatus_id(status);
	}

	public long getStatus() {
		return item.getStatus_id();
	}

	public void setAmount(double amount) {
		item.setAmount(amount);
	}

	public double getAmount() {
		return item.getAmount();
	}

	public void setCurrency(String currency) {
		item.setCurrency(currency);
	}

	public String getCurrency() {
		return item.getCurrency();
	}

	public void reset_data() {
		this.item = new FundingItem();
		this.item_idx = -1;
		this.personIds = null;
	}

	public long[] getPersonIds() {
		return personIds;
	}

	public void setPersonIds(long[] ids) {
//            System.out.println( "\t[][][][]tperson ids:" + ids.length );
		this.personIds = ids;
	}

	public void update_person_ids() {
		ArrayList persons = item.getPersons();
		if (persons == null || persons.size() < 1) {
			this.personIds = new long[0];
			return;
		}

		personIds = new long[persons.size()];
		for (int i = 0; i < persons.size(); i++) {
			Person o = (Person) persons.get(i);
			personIds[i] = o.getId();
		}
	}

	public void populate(HttpServletRequest request, PersistenceToken token) {
		HttpSession session = request.getSession();
		PersonDB acc = PersonDB.getManager(token);
		long[] person_ids = getPersonIds();
		if (person_ids == null) person_ids = new long[0];

		Activity act_bean = FundingEditorAction.activity_lookup(session, this);
		try {
			Vector persons = acc.getByOrg(String.valueOf(getOrgId()));
			ArrayList selected_persons = new ArrayList();
			for (int i = 0; i < persons.size(); i++) {
				Person pi = (Person) persons.elementAt(i);
				for (int j = 0; j < person_ids.length; j++) {
					if (pi.getId() == person_ids[j]) {
						selected_persons.add(pi);
					}
				}
			}
			getItem().setPersons(selected_persons);
		} catch (SQLException sqlex) {
			sqlex.printStackTrace();
		}

		try {
//			item.setStatus(FundingStatusPersMan.getManager(token).
//				 getById(item.getStatus_id()));
//               item.setType( FundingTypePersMan.getManager( token ).
//                  getById( item.getType_id() ) );
			item.setAmount(NumberFormat.getInstance(TranslatorUtils.getCurrentLanguage(request, getServlet().getServletContext()).locale)
				 .parse(HttpUtils.getValidTrimedString(request, "amount", "0")).doubleValue());
			item.setComments(HttpUtils.getValidTrimedString(request, "comment", null));
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		// signature date
		int sy = HttpUtils.getInt(request, "a_sign", 0);
		int sm = HttpUtils.getInt(request, "l_sign", 0);
		int sz = HttpUtils.getInt(request, "z_sign", 0);
		Calendar scal = Calendar.getInstance();
		scal.set(sy, sm, sz, 0, 0, 0);
		item.setSignature_date(scal.getTimeInMillis());

		// type of assistance
		try {
			item.setType_of_assistance(PropertyListDB.getManager(token).getItemById(HttpUtils.getInt(request, "typeofassistanceid", -1)));
		} catch (SQLException sqlex) {
			sqlex.printStackTrace();
		}

		// terms of assistance
		try {
			item.setTerms_of_assistance(PropertyListDB.getManager(token).getItemById(HttpUtils.getInt(request, "termofassistanceid", -1)));
		} catch (SQLException sqlex) {
			sqlex.printStackTrace();
		}

		// status
		try {
			item.setStatus(PropertyListDB.getManager(token).getItemById(HttpUtils.getInt(request, "status", -1)));
		} catch (SQLException sqlex) {
			sqlex.printStackTrace();
		}

		ArrayList commitments = item.getCommitments();
		for (int i = 0; i < commitments.size(); i++) {
			FundingTableItem titem = (FundingTableItem) commitments.get(i);
			titem.setYear(HttpUtils.getInt(request, "commyear" + i, 1998));
			try {
				titem.setAmount(NumberFormat.getInstance(TranslatorUtils.getCurrentLanguage(request, getServlet().getServletContext()).locale)
					 .parse(HttpUtils.getValidTrimedString(request, "camount" + i, "0")).doubleValue());
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			titem.setCurrency(HttpUtils.getValidTrimedString(request, "ccurrency" + i, "USD"));
			Calendar cal = Calendar.getInstance();
			int y = HttpUtils.getInt(request, "ca" + i, 0);
			int m = HttpUtils.getInt(request, "cl" + i, 0);
			int z = HttpUtils.getInt(request, "cz" + i, 0);
			cal.set(y, m, z, 0, 0, 0);
			titem.setDate(cal.getTimeInMillis());
		}

		ArrayList disbursements = item.getDisbursements();
		for (int i = 0; i < disbursements.size(); i++) {
			FundingTableItem titem = (FundingTableItem) disbursements.get(i);
			titem.setYear(HttpUtils.getInt(request, "disbyear" + i, 1998));
			try {
				titem.setAmount(NumberFormat.getInstance(TranslatorUtils.getCurrentLanguage(request, getServlet().getServletContext()).locale)
					 .parse(HttpUtils.getValidTrimedString(request, "damount" + i, "0")).doubleValue());
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			titem.setCurrency(HttpUtils.getValidTrimedString(request, "dcurrency" + i, "USD"));
			Calendar cal = Calendar.getInstance();
			int y = HttpUtils.getInt(request, "da" + i, 0);
			int m = HttpUtils.getInt(request, "dl" + i, 0);
			int z = HttpUtils.getInt(request, "dz" + i, 0);
			cal.set(y, m, z, 0, 0, 0);
			titem.setDate(cal.getTimeInMillis());
		}
	}
}
